using Lzfy_His_Service.Models;
using Lzfy_His_Service.Services;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Lzfy_His_Service.Controllers
{
    /// <summary>
    /// 患者沟通记录查询控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class PatientCommunicationController : ControllerBase
    {
        private readonly IPatientCommunicationService _patientCommunicationService;
        private readonly ILogger<PatientCommunicationController> _logger;

        public PatientCommunicationController(
            IPatientCommunicationService patientCommunicationService,
            ILogger<PatientCommunicationController> logger)
        {
            _patientCommunicationService = patientCommunicationService;
            _logger = logger;
        }

        /// <summary>
        /// 通过患者电话查询沟通记录
        /// </summary>
        /// <param name="request">查询请求</param>
        /// <returns>患者沟通记录列表</returns>
        [HttpPost("query")]
        public async Task<IActionResult> QueryCommunicationRecords([FromBody] PatientCommunicationQueryRequest request)
        {
            try
            {
                // 参数验证
                if (request == null || string.IsNullOrWhiteSpace(request.PhoneNumber))
                {
                    return BadRequest(new
                    {
                        success = false,
                        message = "患者电话号码不能为空",
                        data = default(object?)
                    });
                }

                // 查询沟通记录
                var records = await _patientCommunicationService.GetCommunicationRecordsByPhoneAsync(request.PhoneNumber);

                _logger.LogInformation("患者沟通记录查询成功，电话: {PhoneNumber}，记录数: {Count}", request.PhoneNumber, records.Count);

                return Ok(new
                {
                    success = true,
                    message = "查询成功",
                    data = records
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "患者沟通记录查询失败，电话: {PhoneNumber}", request?.PhoneNumber);

                return StatusCode(500, new
                {
                    success = false,
                    message = "查询失败，请稍后重试",
                    data = default(object?)
                });
            }
        }
    }
}